package metalink.student;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;

public class EditImage extends ActionSupport implements ServletRequestAware {
	private File image;
	private String image_name;
	private String userImage;
    public String getUserImage() {
		return userImage;
	}

	public void setUserImage(String userImage) {
		this.userImage = userImage;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	private HttpServletRequest servletRequest;
	
	public String getImage_name() {
		return image_name;
	}

	public void setImage_name(String image_name) {
		this.image_name = image_name;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String execute() throws Exception {
		try {
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			String image_editor_email_id = session.getAttribute("username").toString();
			if (image != null){
	
			Connect c = new Connect();
			String photo = "attachments/"+image_editor_email_id + ".jpg";
			c.dml("insert into profile_image(image_editor_email_id,image_name) values('"
					+ image_editor_email_id + "','" + photo + "')");
			userImage = image_editor_email_id + ".jpg";
			 @SuppressWarnings("deprecation")
			String filePath = servletRequest.getRealPath("/attachments");
			
			File f = new File(
					filePath, userImage);
			
			FileUtils.copyFile(image, f);
			}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return ERROR;
			}
			
			
			return SUCCESS;
			
			
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}

}
